package com.awt.layoutManager;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @authoer zsj
 * @date 2023/4/15 13:21
 */
public class CardLayoutDemo {
    public static void main(String[] args) {
        Frame frame = new Frame();

        CardLayout cardLayout = new CardLayout();
        Panel container = new Panel(cardLayout);
        String[] name = {"第1张", "第2张", "第3张", "第4张", "第5张"};
        for (int i = 0; i < name.length; i++) {
            container.add(name[i], new Button(name[i]));
        }
        frame.add(container);

        Panel foot = new Panel();
        Button b1 = new Button("上一张");
        Button b2 = new Button("下一张");
        Button b3 = new Button("第一张");
        Button b4 = new Button("最后一张");
        Button b5 = new Button("第三张");

        //创建点击事件监听器
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                switch (e.getActionCommand()) {
                    case "上一张":
                        cardLayout.previous(container);
                        break;
                    case "下一张":
                        cardLayout.next(container);
                        break;
                    case "第一张":
                        cardLayout.first(container);
                        break;
                    case "最后一张":
                        cardLayout.last(container);
                        break;
                    case "第三张":
                        cardLayout.show(container,"第3张");
                        break;
                }
            }
        };

        b1.addActionListener(listener);
        b2.addActionListener(listener);
        b3.addActionListener(listener);
        b4.addActionListener(listener);
        b5.addActionListener(listener);
        foot.add(b1);
        foot.add(b2);
        foot.add(b3);
        foot.add(b4);
        foot.add(b5);
        frame.add(foot, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
    }
}
